/*****************************************************************************/
/* File: HighScoreState.h                                                    */
/* Author: rszulgo                                                           */
/*                                                                           */
/* Description:																 */
/* Specialization of the CState class for									 */
/* the high scores state. This displays the high scores (player name+score). */
/* When a new high score is available after a game, it lets the				 */
/* player enters his name.													 */
/*****************************************************************************/
#pragma once

#include "State.h"
#include "Font.h"
#include "GameState.h"
#include "TextControl.h"
#include "Picture.h"
#include <vector>

class CHighScoreState : public CState
{
public:
	~CHighScoreState(void);
	static CHighScoreState* GetInstance(CStateManager* pManager, CRenderer* pRenderer, HDC hDC, HWND hWnd);
	void getKeyPressed();
	void Draw();
	void EnterState();

	// Sets a new score: if this score should be 
	// part of the high scores, the user will need 
	// to enter his name.
	void SetNewHighScore1(ULONG ulNewHighScore)  { m_ulNewHighScore1 = ulNewHighScore; }
	void SetNewHighScore2(ULONG ulNewHighScore)  { m_ulNewHighScore2 = ulNewHighScore; }

protected:
	CHighScoreState(CStateManager* pManager, CRenderer* pRenderer, HDC hDC, HWND hWnd);

private:
	// Saves the current high scores in a file highscores.txt
	void SaveScores();

	// Adds a new score in the high-score table and
	// insert it at the correct location.
	void AddNewScore(const std::string& strName, ULONG ulScore);

	//On Char event
	void CHighScoreState::OnChar();	

	// High-score data: score and player name.
	struct HighScoreData
	{
		std::string strPlayer;
		ULONG ulScore;

		// We have to sort in decreasing order, so the <
		// operator returns the opposite.
		bool operator< (const HighScoreData& other)
		{
			if  (this->ulScore > other.ulScore) 
				return true;
			return false;
		}
	};

	// The new high-score, if any, for player 1 and player 2
	ULONG m_ulNewHighScore1;
	ULONG m_ulNewHighScore2;

	// The font used to draw text
	CFont* m_pWritingFont;

	// Mode in which the user has to enter his name.
	bool m_bEnterName;
	
	// Char array containing the name currently being entered.
	char m_pCurrentName[26];
	
	// The index of the next char to be entered.
	int m_iNameIndex;
	
	typedef std::vector<HighScoreData> THighScoreTable;
	
	// The high-score table.
	THighScoreTable m_vecHighScores;
	
	// The background image
	TPicturePtr m_pBackgroundImg;

	// The Enter Name image
	TPicturePtr m_pEnterNameImg;

	//back text control
	CTextControl* m_pBackControl;
};